﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Dynamic;
using EIIS.Infrastructure.Repositories;
using EIIS.Infrastructure.Repositories.System;
using EIIS.Infrastructure.Model.System;

namespace EIIS.Infrastructure.Repositories.Implement
{
    public class DictionaryRepository : RepositoryBase<Dictionary>, IDictionaryRepository
    {
        public IQueryable<Dictionary> LoadAllByPage(out long total, string strDicCategoryId, int page, int rows, string order, string sort)
        {
            IQueryable<Dictionary> list;
            if (!string.IsNullOrEmpty(strDicCategoryId))
            {
                list = this.LoadAll().Where(d => d.Category != null && d.Category.Id == strDicCategoryId);
            }
            else
            {
                list = this.LoadAll();
            }
            total = list.LongCount();
            list = list.OrderBy(d => d.Category.Id);
            if (!string.IsNullOrEmpty(sort) && !string.IsNullOrEmpty(order))
            {
                list = list.OrderBy(sort + " " + order);
            }
            list = list.Skip((page - 1) * rows).Take(rows);

            return list;
        }

        public IQueryable<Dictionary> LoadAllByCategoryCode(string strCode)
        {
            var list = this.LoadAll().Where(d => d.Category != null && d.Category.TreeCode == strCode).OrderBy(d => d.Index);
            return list;
        }

        public Dictionary GetByTiaoMH(string TiaoMH, int ZT)
        {
            Dictionary dic = null;

            string strHql = "select d from Dictionary as d,Package as p where d.Id =p.BaoMC and p.TiaoMH=:TiaoMH and p.ZhuangT=:ZhuangT";
            var query = Session.CreateQuery(strHql)
                .SetString("TiaoMH", TiaoMH)
                .SetInt32("ZhuangT",ZT)
                .List<Dictionary>();
            if (query != null && query.Count > 0) dic = query[0];
            return dic;
        }

    }
}
